/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Interfaz.java
 *
 * Created on Dec 23, 2011, 3:19:04 PM
 */

package compresor;
import javax.swing.JFileChooser;

/**
 *
 * @author Xavier
 */
public class Interfaz extends javax.swing.JFrame {

    boolean isOrigen=false, isDestino=false;
    /** Creates new form Interfaz */
    public Interfaz() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        pnlOrigen = new javax.swing.JPanel();
        txtOrigen = new javax.swing.JTextField();
        btnOrigen = new javax.swing.JButton();
        lblOrigen = new javax.swing.JLabel();
        pnlDestino = new javax.swing.JPanel();
        txtDestino = new javax.swing.JTextField();
        btnDestino = new javax.swing.JButton();
        lblDestino = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();
        btnComprimir = new javax.swing.JButton();
        jtbComprimir = new javax.swing.JRadioButton();
        jtbDescomprimir = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Comprimir");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnOrigen.setText("...");
        btnOrigen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrigenActionPerformed(evt);
            }
        });

        lblOrigen.setText("Origen");

        javax.swing.GroupLayout pnlOrigenLayout = new javax.swing.GroupLayout(pnlOrigen);
        pnlOrigen.setLayout(pnlOrigenLayout);
        pnlOrigenLayout.setHorizontalGroup(
            pnlOrigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlOrigenLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlOrigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlOrigenLayout.createSequentialGroup()
                        .addComponent(txtOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22))
                    .addGroup(pnlOrigenLayout.createSequentialGroup()
                        .addComponent(lblOrigen)
                        .addContainerGap(268, Short.MAX_VALUE))))
        );
        pnlOrigenLayout.setVerticalGroup(
            pnlOrigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlOrigenLayout.createSequentialGroup()
                .addComponent(lblOrigen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlOrigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOrigen))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnlOrigen, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 310, 50));

        btnDestino.setText("...");
        btnDestino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDestinoActionPerformed(evt);
            }
        });

        lblDestino.setText("Destino");

        javax.swing.GroupLayout pnlDestinoLayout = new javax.swing.GroupLayout(pnlDestino);
        pnlDestino.setLayout(pnlDestinoLayout);
        pnlDestinoLayout.setHorizontalGroup(
            pnlDestinoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDestinoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDestinoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDestinoLayout.createSequentialGroup()
                        .addComponent(txtDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22))
                    .addGroup(pnlDestinoLayout.createSequentialGroup()
                        .addComponent(lblDestino)
                        .addContainerGap(264, Short.MAX_VALUE))))
        );
        pnlDestinoLayout.setVerticalGroup(
            pnlDestinoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDestinoLayout.createSequentialGroup()
                .addComponent(lblDestino)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDestinoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDestino))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnlDestino, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 50, 310, 50));

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        getContentPane().add(btnSalir, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 120, -1, -1));

        btnComprimir.setText("Comprimir");
        btnComprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnComprimirActionPerformed(evt);
            }
        });
        getContentPane().add(btnComprimir, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 120, -1, -1));

        jtbComprimir.setSelected(true);
        jtbComprimir.setText("Comprimir");
        jtbComprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbComprimirActionPerformed(evt);
            }
        });
        getContentPane().add(jtbComprimir, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, -1, -1));

        jtbDescomprimir.setText("Descomprimir");
        jtbDescomprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbDescomprimirActionPerformed(evt);
            }
        });
        getContentPane().add(jtbDescomprimir, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnComprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnComprimirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnComprimirActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        //Cierra toda la aplicacion
        System.exit(0);
    }//GEN-LAST:event_btnSalirActionPerformed

    // Validacion de los RadioButton para que solo se selecciones 1
    private void jtbComprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbComprimirActionPerformed
        if(jtbComprimir.isSelected())
            jtbDescomprimir.setSelected(false);
        else
            jtbComprimir.setSelected(true);
    }//GEN-LAST:event_jtbComprimirActionPerformed

    private void jtbDescomprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbDescomprimirActionPerformed
        if(jtbDescomprimir.isSelected())
            jtbComprimir.setSelected(false);
        else
            jtbDescomprimir.setSelected(true);
    }//GEN-LAST:event_jtbDescomprimirActionPerformed

    private void btnOrigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrigenActionPerformed
        // Carga la ruta del archivo a comprimir en el txtOrigen.
        isOrigen=true;
        txtOrigen.setText(Ruta());
    }//GEN-LAST:event_btnOrigenActionPerformed

    private void btnDestinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDestinoActionPerformed
        // TODO add your handling code here:
        isDestino=true;
        txtDestino.setText(Ruta());
    }//GEN-LAST:event_btnDestinoActionPerformed
    public String Ruta(){
        String ruta=null;
        int resultado = 0;
        JFileChooser ventana = new JFileChooser();// creamos la ventana
        ventana.setDialogTitle("Archivo");
        ventana.setMultiSelectionEnabled(false);//inabilitamos la multiseleccion
        if(isOrigen==true)
            resultado = ventana.showOpenDialog(null);//habilitamos la seleccion
        else if(isDestino==true)
            resultado = ventana.showSaveDialog(null);//habilitamos ruta donde se va a guardar
        //si se presiono el boton abrir o guardar
        if ( resultado == JFileChooser.APPROVE_OPTION )
               	ruta = ventana.getSelectedFile().getAbsolutePath();

        isOrigen=isDestino=false;
        return ruta;
    }
    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnComprimir;
    private javax.swing.JButton btnDestino;
    private javax.swing.JButton btnOrigen;
    private javax.swing.JButton btnSalir;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JRadioButton jtbComprimir;
    private javax.swing.JRadioButton jtbDescomprimir;
    private javax.swing.JLabel lblDestino;
    private javax.swing.JLabel lblOrigen;
    private javax.swing.JPanel pnlDestino;
    private javax.swing.JPanel pnlOrigen;
    private javax.swing.JTextField txtDestino;
    private javax.swing.JTextField txtOrigen;
    // End of variables declaration//GEN-END:variables

}
